package router

import (
	"iris-wms/apps/admin/web/dto/din"
	"iris-wms/apps/admin/web/service"
	"iris-wms/common/model"
	"iris-wms/common/tools"
	"iris-wms/common/tools/result"

	"github.com/kataras/iris/v12"
)

func NewAdminButtonRoute() *adminButtonRoute {
	return &adminButtonRoute{}
}

type adminButtonRoute struct{}

func (r *adminButtonRoute) Create(ctx iris.Context, service_ service.AdminButtonService) {
	var in model.AdminButton
	if err := ctx.ReadJSON(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	in.Uid = tools.GetUserID(ctx)
	if err := service_.Create(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *adminButtonRoute) Edit(ctx iris.Context, service_ service.AdminButtonService) {
	var in model.AdminButton
	if err := ctx.ReadJSON(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	if err := service_.Update(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *adminButtonRoute) Delete(ctx iris.Context, service_ service.AdminButtonService) {
	var idList = make([]uint, 0)
	if err := ctx.ReadJSON(&idList); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	if err := service_.DeleteByIdList(idList); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *adminButtonRoute) Generate(ctx iris.Context, service_ service.AdminButtonService) {
	if err := service_.Generate(tools.GetUserID(ctx)); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *adminButtonRoute) Table(ctx iris.Context, service_ service.AdminButtonService) {
	var in din.VxeTableIn
	if err := ctx.ReadQuery(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	rows, total, err := service_.GetTable(in)
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.VxeTableData(ctx, in.PageSize, in.CurrentPage, total, rows, nil)
}

func (r *adminButtonRoute) List(ctx iris.Context, service_ service.AdminButtonService) {
	rows, err := service_.GetList()
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OKData(ctx, rows)
}
